package com.hmkj.admin.controller.user;


import com.hmkj.admin.controller.BaseController;
import com.hmkj.admin.utils.AdminSessionHelper;
import com.hmkj.common.baseMapper.GenericPo;
import com.hmkj.core.model.amount.AccountModel;
import com.hmkj.core.model.user.UserModel;
import com.hmkj.core.po.amount.Account;
import com.hmkj.core.po.amount.AccountLog;
import com.hmkj.core.po.operate.Agent;
import com.hmkj.core.po.system.Admin;
import com.hmkj.core.po.system.Bank;
import com.hmkj.core.po.user.User;
import com.hmkj.core.service.amount.AccountLogService;
import com.hmkj.core.service.amount.AccountService;
import com.hmkj.core.service.operate.AgentService;
import com.hmkj.core.service.system.BankService;
import com.hmkj.core.service.user.UserService;
import com.xiaoleilu.hutool.util.StrUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(BaseController.BASE_URI)
public class UserController extends BaseController {

    @Resource
    private UserService userService;
    @Resource
    private AccountService accountService;
    @Resource
    private AccountLogService accountLogService;
    @Resource
    private BankService bankService;
    @Resource
    private AgentService agentService;

    /**
     * 用户列表
     *
     * @return
     */
    @RequestMapping("user/list")
    public String list() {
        return ok("user/user/list");
    }


    /**
     * 用户列表
     *
     * @return
     */
    @RequestMapping("user/detail")
    public String detail(ModelMap modelMap, Integer userId) {

        // 用户信息
        User user = userService.selectByPrimaryKey(userId);
        UserModel userModel = new UserModel();
        BeanUtils.copyProperties(user,userModel);
        //邀请人用户名
        User user1  = new User();
        if (user.getParentId()!=null){
            user1 = userService.selectByPrimaryKey(user.getParentId());
            if (user1!=null){
                userModel.setP(user1);
            }else{
                user1  = new User();
                user1.setUserName("");
                userModel.setP(user1);
            }
        }else{
            user1.setUserName("");
            userModel.setP(user1);
        }
        //隐藏部分手机号
        if(user.getMobile()!=null){
            userModel.setMobileHide(StrUtil.sub(user.getMobile(), 0, 2) + "****" + StrUtil.sub(user.getMobile(), 9, user.getMobile().length()));
        }
        //邀请人数
        userModel.setInviteNumber(userService.inviteNumber(userId));
        modelMap.put("user", userModel);

        // 用户账户信息
        Account account = accountService.selectOne(new Account(t -> t.setUserId(userId)));
        AccountModel model = new AccountModel();
        BeanUtils.copyProperties(account,model);
        model.setRechargeSum(accountLogService.selectSumByType(AccountLog.TYPE.T1,AccountLog.TYPE.T12, userId));
        model.setCashSum(accountLogService.selectSumByType(AccountLog.TYPE.T9,AccountLog.TYPE.T13, userId));
        //累计获取推广佣金
        model.setRewardSum(accountLogService.selectSumByAirectionType(AccountLog.DIRECTION.RECEIVE,AccountLog.TYPE.T11,userId));
        modelMap.put("accountModel", model);

        // 银行卡列表
        List<Bank> banks = bankService.select(new Bank(t->t.setDelFlag(GenericPo.DELFLAG.NORMAL.code)));
        modelMap.put("banks", banks);
        List<Agent> agents = new ArrayList<>();
        Admin admin = AdminSessionHelper.getCurrAdmin();
        if(admin.getType()==Admin.TYPE.T3.code){
            agents  = agentService.select(new Agent(t->{t.setStatus(Agent.STATUS.T1.code);t.setId(admin.getBussId());t.setDelFlag(GenericPo.DELFLAG.NORMAL.code);}));
        }else {
            agents  = agentService.select(new Agent(t->{t.setStatus(Agent.STATUS.T1.code);t.setDelFlag(GenericPo.DELFLAG.NORMAL.code);}));
        }
        // 代理商列表
        modelMap.put("agents", agents);
        return ok("user/user/detail");
    }

    /**
     * 用户来源统计
     *
     */
    @RequestMapping("user/userForm")
    public String userForm(ModelMap modelMap) {
        //用户来源类型
        List<String> sources = userService.getSource();
        modelMap.put("sources", sources);
        return ok("form/userForm");
    }



}
